package com.hy.proxy.common;

import java.io.*;

/**
 * @Author: hy
 * @Date: Created in 2019/10/7/19:38
 * @Description: 常用对文件的操作工具类
 * @Version: 1.0
 */
public class FileUtils {

    /**
     * 获取一个文件的内容
     *
     * @param file
     * @return
     * @throws IOException
     */
    public static String readFile(File file) throws IOException {

        if (file.isFile()) {

            StringBuilder sb = new StringBuilder();

            String temp;

            BufferedReader bufferedReader = new BufferedReader(new FileReader(file));

            while ((temp = bufferedReader.readLine()) != null) {
                sb.append(temp);
            }

            bufferedReader.close();

            return sb.toString();

        } else {
            throw new IOException("路径 " + file.getPath() + " 不是一个文件。");
        }
    }

    /**
     * 写出一个文件到指定路径
     * @param str 写出内容
     * @param path 写出路径
     * @throws IOException
     */
    public static void writeFileByString(String str,File path) throws IOException {

        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(path));

        bufferedWriter.write(str);

        bufferedWriter.close();
    }
}
